package com.deltapunkt.proxy;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;

public class TaskScheduler implements Runnable {
	private BlockingDeque<Runnable> taskQueue;
	private Executor exec;

	public TaskScheduler(int n) {
		taskQueue = new LinkedBlockingDeque<Runnable>(n);
		exec = Executors.newFixedThreadPool(n + 1);
		exec.execute(this);
	}

	@Override
	public void run() {
		while (true) {
			try {
				exec.execute(taskQueue.take());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void addTask(Proxy proxy){
		try {
			taskQueue.put(proxy);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
